package com.atguigu.wuliupublisher.controller;


import com.atguigu.wuliupublisher.bean.TrandsReceiveCt;
import com.atguigu.wuliupublisher.bean.TrandsReceiveSucCt;
import com.atguigu.wuliupublisher.service.TrandsRecriveService;
import com.atguigu.wuliupublisher.util.DateFormatUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class TrandsRecriveController {
    @Autowired
    TrandsRecriveService trandsRecriveService;
    @RequestMapping("/topN")
    public String getTopN(@RequestParam(value = "date",defaultValue = "0")Integer date){
        if (date == 0){
            date= DateFormatUtil.now();
        }
        List<TrandsReceiveCt> trandsReceiveCtList = trandsRecriveService.getTrandsReceiveCt(date);
        StringBuilder topNBuilder = new StringBuilder("{\"status\": 0,\"data\": {\"categories\": [");
        StringBuilder zhongjian = new StringBuilder("    ],\"series\": [{\"name\": \"揽收量 .3\",\"data\": [");
        for (int i = 0; i < trandsReceiveCtList.size(); i++) {
            TrandsReceiveCt trandsReceiveCt = trandsReceiveCtList.get(i);
            topNBuilder.append("\""+trandsReceiveCt.getOrgName()+"\"");
             if (i < trandsReceiveCtList.size()-1){
                 topNBuilder.append(",");
             }
             zhongjian.append("\""+trandsReceiveCt.getOrderCt()+"\"");
            if (i < trandsReceiveCtList.size()-1)
                 zhongjian.append(",");

        }

        topNBuilder.append(zhongjian).append("]}]}}");
        return topNBuilder.toString();
    }

    @RequestMapping("/province")
    public String getProvine(@RequestParam(value = "date",defaultValue = "0")Integer date) {
        if (date == 0) {
            date= DateFormatUtil.now();
        }
        List<TrandsReceiveSucCt> trandsReceiveSucCt = trandsRecriveService.getTrandsReceiveSucCt(date);
        StringBuilder stringBuilder = new StringBuilder("{\"status\": 0,\"data\": {\"mapData\": [");
        for (int i = 0; i < trandsReceiveSucCt.size(); i++) {
            TrandsReceiveSucCt bean = trandsReceiveSucCt.get(i);
            stringBuilder.append("{\"name\": \""+ bean.getProvinceName()+ "\",\"value\":"+bean.getDeScCt()+"}");
            if (i<trandsReceiveSucCt.size()-1){
                stringBuilder.append(",");
            }
        }
        stringBuilder.append("  ],\"valueName\": \"派送成功运单数\"}}");
        return stringBuilder.toString();
    }
}
